import os
from typing import List


class FileScanner:

    def scan(self, root_path: str, scan_deep: bool = False, call_back=None, filter_suffix: str = None) -> List[str]:
        if scan_deep:
            return self.scan_deep(root_path, call_back, filter_suffix)
        else:
            return self.scan_current_folder(root_path, call_back, filter_suffix)

    def scan_current_folder(self, root_path: str, call_back=None, filter_suffix: str = None) -> List[str]:
        all_file_path = []
        for file_or_folder_name in os.listdir(root_path):
            file_path = os.path.join(root_path, file_or_folder_name)
            if filter_suffix:
                if not file_path.endswith(filter_suffix):
                    continue
            if not os.path.exists(file_path):
                continue
            if not os.path.isfile(file_path):
                continue
            all_file_path.append(file_path)
            if call_back:
                call_back(file_path)
        return all_file_path

    def scan_deep(self, root_path: str, call_back=None, filter_suffix: str = None) -> List[str]:
        all_file_path = []
        path = root_path.replace("/", os.sep)
        for (root, dirs, files) in os.walk(path, topdown=True):
            for file in files:
                file_path = os.path.join(root, file)
                if filter_suffix:
                    if not file_path.endswith(filter_suffix):
                        continue
                if not os.path.exists(file_path):
                    continue
                all_file_path.append(file_path)
                if call_back:
                    call_back(file_path)
        return all_file_path
